﻿<?php
	error_reporting(1);
	include './includes/connectdb.php';
	include './classes/mysql.class.php';

	
	if (isset($_GET ['pid']))
	{
		$pid = $_GET['pid'];
	}
	$uploaddir = './img/avatars/';
	$filename = $_FILES['uploadfile']['name'];
	$file = $uploaddir . basename($_FILES['uploadfile']['name']);
	$ext = substr($_FILES['uploadfile']['name'],strpos($_FILES['uploadfile']['name'],'.'),strlen($_FILES['uploadfile']['name'])-1);
		
	$filetypes = array('.jpg','.gif','.bmp','.png','.JPG','.BMP','.GIF','.PNG','.jpeg','.JPEG');
	
	$temp = explode(".", $_FILES["uploadfile"]["name"]);
	$newfilename = reset($temp). date("Ymd-His") . '.' . end($temp);
	
	
	//error_log("L'ID est le suivant $id", 0);
	
	
	if(!in_array($ext,$filetypes))
	{
		echo "formatError";
	}
	else
	{
		if(empty($_FILES['uploadfile']) || $_FILES['uploadfile']['size'] > 500*1048)
		{
			echo "sizeError";
		}
		else if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $uploaddir . $newfilename)) 
		{
			$imginfo = getimagesize($file);
			if ($imginfo[2] == '1' || $imginfo[2] == '2' || $imginfo[2] == '3')
			{		
				echo $newfilename;
			}
			else
			{
				unlink($file);
				echo $newfilename;
			}
		}
		else 
		{
			echo "error";
		}
		}
		?>		